from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync


def send_real_time_notification(user_id: int, notification_data: dict):
    """
    发送实时通知（在线用户立即接收，离线用户存入DB）
    """
    channel_layer = get_channel_layer()

    # 通过WebSocket发送给在线用户
    async_to_sync(channel_layer.group_send)(
        f"notify_{user_id}",
        {
            "type": "notify_message",
            "content": {"event": "new_notification", "data": notification_data},
        },
    )
